<?php

class Publisher extends Model_Abstract
{
	public function getAllPublishers()
	{
		try
		{
			$sql = 'select * from publishers';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function add($name)
	{
		try
		{
			$sql = 'insert into publishers set name=?';
			$result = $this->_connection->execute($sql, array($name), true);
			return $result;			
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getSubs($pubid)
	{
		try
		{
			$sql = 'select * from subs where pub_id=?';
			$result = $this->_connection->query($sql, array($pubid));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getPublisherById($id)
	{
		try
		{
			$sql = 'select * from publishers where id=? limit 1';
			$result = $this->_connection->query($sql, array($id));
			if (count($result) == 1)
			{
				return $result[0];
			}
			else
			{
				return false;
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function addSub($pubid, $name)
	{
		try
		{
			$sql = 'insert into subs set pub_id=?, name=?';
			$this->_connection->execute($sql, array($pubid, $name));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getAllPubSubs()
	{
		try
		{
			$sql = 'select p.id as id, p.name as name, s.id as sub_id, s.name as sub_name from publishers p left join subs s on s.pub_id=p.id';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
}
?>